<?php

namespace app\logic;

use app\models\Field;
use yii\db\ActiveRecord;
use app\forms\FieldForm;
use yii\web\HttpException;
use yii\db\StaleObjectException;
use app\components\helpers\ArrayHelper;

/**
 * 字段逻辑类
 */
class FieldLogic extends BaseLogic
{
    /**
     * 首页列表 字段
     * @param $param
     * @return array|bool|int|string|ActiveRecord|ActiveRecord[]|null
     * @throws HttpException
     */
    public static function index($param)
    {
        $form = self::_validateForm($param, 'index');
        return Field::getListExt($form->getAttributes());
    }

    /**
     * 创建 字段
     * @param $param
     * @throws HttpException
     */
    public static function create($param)
    {
        $form = self::_validateForm($param, 'create');
        $attr = ArrayHelper::humpToUnderline($form->getAttributes());
        Field::newCreate($attr);
        throw new HttpException(200);
    }

    /**
     * 编辑 字段
     * @param $param
     * @throws HttpException
     */
    public static function update($param)
    {
        $form = self::_validateForm($param, 'update');
        $attr = ArrayHelper::humpToUnderline($form->getAttributes());
        $id = $attr['id'];
        unset($attr['id'], $attr['page'], $attr['page_size']);
        $rs = Field::updateByPk($attr, $id);
        throw new HttpException(200);
    }

    /**
     * 删除 字段
     * @param $param
     * @throws HttpException
     * @throws \Throwable
     * @throws StaleObjectException
     */
    public static function delete($param)
    {
        $form = self::_validateForm($param, 'delete');
        $model = Field::findOne($form['id']);
        $model->delete();
        throw new HttpException(200);
    }

    /**
     * 禁用、启用 字段
     * @param $param
     * @throws HttpException
     */
    public static function status($param)
    {
        $form = self::_validateForm($param, 'status');
        $model = Field::findOne($form['id']);
        $model->status = $form['status'];
        $model->save(false);
        throw new HttpException(200);
    }

    /**
     * 权限选择 字段
     * @param $param
     * @throws HttpException
     */
    public static function auth($param)
    {
        $form = self::_validateForm($param, 'auth');
        $model = Field::findOne($form['id']);
        $model->auth = $form['auth'];
        $model->save(false);
        throw new HttpException(200);
    }

    /**
     * 编辑时的信息 字段
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function getInfo($param)
    {
        $form = self::_validateForm($param, 'get-info');
        $column = [
            'module',
            'type',
            'form_type AS formType',
            'name',
            'max_length AS maxLength',
            'default_value AS defaultValue',
            'value',
            'tips',
            'is_validate AS isValidate',
            'is_unique AS isUnique',
            'is_check AS isCheck',
            'is_allow_empty AS isAllowEmpty',
            'in_list_show AS inListShow',
            'in_add_show AS inAddShow',
            'status',
            'auth',
            'sort',
        ];
        $field = Field::getSingleByPk($form['id'], $column);
        $arr = self::_getParam();
        $arr['field'] = $field;
        return $arr;
    }

    /**
     * 表单参数
     * @return array
     */
    public static function getParam()
    {
        return self::_getParam();
    }

    /**
     * 参数、表单数据 验证
     * @param $param
     * @param $scenario
     * @return FieldForm
     * @throws HttpException
     */
    public static function _validateForm($param, $scenario)
    {
        $form = new FieldForm();
        $form->setScenario($scenario);
        $form->setAttributes($param);
        if (!$form->validate()) {
            throw new HttpException(400, $form->getFirstError());
        }
        return $form;
    }

    /**
     * 参数
     * @return array
     */
    public static function _getParam()
    {
        $typeList = Field::getTypeMapping();
        $validateList = Field::getValidateMapping();
        $checkList = Field::getCheckMapping();
        $listShowList = Field::getListShowMapping();
        $addShowList = Field::getAddShowMapping();
        $moduleList = Field::getModuleMapping();
        $formTypeList = Field::getFormTypeMapping();
        $allowEmptyList = Field::getAllowEmptyMapping();

        return [
            'formTypeList' => $formTypeList,
            'allowEmptyList' => $allowEmptyList,
            'typeList' => $typeList,
            'validateList' => $validateList,
            'checkList' => $checkList,
            'listShowList' => $listShowList,
            'addShowList' => $addShowList,
            'moduleList' => $moduleList,
        ];
    }
}